<script>
import { schedulingStatusArray, settingListArray } from '@/constants/business/waybill'
import InstitutionSelect from '@/components/institutionSelect/institutionSelect.vue'
import FilterPage from '@/components/FilterPage/FilterPage.vue'
import { mapState } from 'vuex'

export default {
  name: 'waybill',
  components: { FilterPage, InstitutionSelect },
  data() {
    return {
      pageParams: {
        page: 1,
        pageSize: 10,
        // 当前所在机构id
        currentAgencyId: undefined,
        // 起始机构ID
        startAgencyId: undefined,
        // 结束机构ID
        endAgencyId: undefined,
        // 订单ID
        orderId: undefined,
        // 运单ID
        id: undefined,
        // 调度状态
        schedulingStatus: undefined,
        // 运单状态
        status: undefined
      },
      loading: false,
      schedulingStatusArray,
      settingListArray,
      startPlaceholder: '请选择起始机构',
      endPlaceholder: '请选择目的机构',
      currentPlaceholder: '请选择所在机构'
    }
  },
  methods: {
    /*
    * 获取运单分页的回调函数
    *  */
    async fetchWaybillList() {
      try {
        this.loading = true
        await this.$store.dispatch('waybillStore/fetchWaybillListAction', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },

    /*
    *
    * 获取起始机构ID
    *  */
    getStartAgencyId({ id, name }) {
      this.pageParams.startAgencyId = id
      this.startPlaceholder = name
    },

    /*
    *
    * 获取结束机构ID
    *  */
    getEndAgencyId({ id, name }) {
      this.pageParams.endAgencyId = id
      this.endPlaceholder = name
    },
    /*
    * 获取当前机构ID
    *  */
    getCurrentAgencyId({ id, name }) {
      this.pageParams.currentAgencyId = id
      this.currentPlaceholder = name
    },

    /*
    *
    * 获取运单状态的回调函数
    *  */
    selectStatus(status) {
      this.pageParams.status = status
      this.fetchWaybillList()
    },
    /*
    *
    * 点击搜索按钮的回调函数
    *  */
    handleSearch() {
      this.fetchWaybillList()
    },

    /*
    *pageSize发生变化的回调函数
    *  */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchWaybillList()
    },
    /*
    *page发生变化的回调函数
    *  */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchWaybillList()
    }

  },
  computed: {
    ...mapState('waybillStore', ['counts', 'waybillList'])
  },
  mounted() {
    this.fetchWaybillList()
  }
}
</script>

<template>
  <div class="waybill">
    <el-card shadow="never" class="top">
      <el-form inline>
        <el-form-item label="运单编号:">
          <el-input placeholder="请输入运单编号" v-model="pageParams.id"></el-input>
        </el-form-item>
        <el-form-item label="订单编号:">
          <el-input placeholder="请输入订单编号" v-model="pageParams.orderId"></el-input>
        </el-form-item>
        <el-form-item label="调度状态:">
          <el-select v-model="pageParams.schedulingStatus">
            <el-option v-for="(item) in schedulingStatusArray" :value="item.type" :label="item.label" :key="item.type"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item label="起始机构:">
          <institution-select :placeholder="startPlaceholder" @nodeClick="getStartAgencyId"></institution-select>
        </el-form-item>
        <el-form-item label="目的机构:">
          <institution-select :placeholder="endPlaceholder" @nodeClick="getEndAgencyId"></institution-select>
        </el-form-item>
        <el-form-item label="所在机构:">
          <institution-select :placeholder="currentPlaceholder" @nodeClick="getCurrentAgencyId"></institution-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <filter-page class="filter-page" :status-list="settingListArray" @select="selectStatus"></filter-page>

    <el-card shadow="never" class="bottom" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255,255,255)">
      <el-table stripe border :data="waybillList" :header-cell-style="{background:'#f4f6f8',color:'#969393'}">
        <el-table-column type="index" label="序号" align="center" width="50px"></el-table-column>
        <el-table-column prop="id" label="运单编号" align="center" width="150px"></el-table-column>
        <el-table-column prop="orderId" label="订单编号" align="center" width="170px"></el-table-column>
        <el-table-column label="运单状态" align="center" width="130px">
          <template #default="{row}">
            <span v-if="row.status == 1">新建</span>
            <span v-else-if="row.status == 2">已装车</span>
            <span v-else-if="row.status == 3">到达</span>
            <span v-else-if="row.status == 4">到达终端网点</span>
            <span v-else-if="row.status == 5">已签收</span>
            <span v-else>拒收</span>
          </template>
        </el-table-column>
        <el-table-column prop="schedulingStatus" label="调度状态" align="center">
          <template #default="{row}">
            <span v-if="row.schedulingStatus == 1">待调度</span>
            <span v-else-if="row.schedulingStatus == 2">未匹配线路</span>
            <span v-else>已调度</span>
          </template>
        </el-table-column>
        <el-table-column prop="startAgencyName" label="起点营业部" align="center" width="130px"></el-table-column>
        <el-table-column prop="endAgencyName" label="终点营业部" align="center" width="130px"></el-table-column>
        <el-table-column prop="currentAgencyName" label="所在机构" align="center" width="110px"></el-table-column>
        <el-table-column prop="nextAgencyName" label="下一个机构" align="center" width="120px"></el-table-column>
        <el-table-column prop="totalVolume" label="货品总体积(立方)" align="center" width="130px"></el-table-column>
        <el-table-column prop="totalWeight" label="货品总重量(千克)" align="center" width="130px"></el-table-column>
        <el-table-column prop="created" label="创建时间" align="center" width="160px"></el-table-column>
        <el-table-column prop="updated" label="更新时间" align="center" width="160px"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="{row}">
            <el-button type="text" @click="$router.push({ path:'/waybill-detail',query:{ id:row.id,orderId:row.orderId }})">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 15, 20, 25]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="counts" background></el-pagination>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.waybill {
  padding: 20px;

  .top {
    margin-bottom: 10px;

    .el-form-item {
      width: 380px;

      .el-input {
        width: 140%;
      }

      .el-select {
        width: 130%;
      }
    }
  }

  .filter-page {
    margin-bottom: 10px;
  }

  .bottom {
    .el-table {
      margin-bottom: 10px;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
